// pages/search/index.js
import { request } from "../../request/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: [],
    inputvalue: "",
    ishidden: false
  },
  timeout: 0,
  handleSearch(e) {
    const inputvalue = e.detail.value
    this.setData({ inputvalue })
    clearTimeout(this.timeout)
    this.timeout = setTimeout(() => {
      this.getSerach(inputvalue)
    }, 1000)

  },
  async getSerach(query) {
    const res = await request({ url: "https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch", data: { query } })
    const search = res.data.message


    this.setData({
      search
    })
    if (search.length === 0) {
      this.setData({
        ishidden: true
      })
    } else {
      this.setData({
        ishidden: false
      })
    }
  },
  clearall() {
    this.setData({
      inputvalue: "",
      search: [],
      ishidden: false
    })
  }
})